package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.domain.AlpayMchntInfo;
import cc.rengu.redp.bizimpl.upmp.dao.AlpayMchntInfoMapper;
import cc.rengu.redp.bizimpl.upmp.service.IAlpayMchntInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘玉亮.LL
 */
@Service
public class AlpayMchntInfoServiceImpl extends ServiceImpl<AlpayMchntInfoMapper, AlpayMchntInfo> implements IAlpayMchntInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Override
    public AlpayMchntInfo findOneByMchntNo(String mchntNo) {
        LambdaQueryWrapper<AlpayMchntInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlpayMchntInfo::getMchntNo,mchntNo);
        return this.getOne(wrapper);
    }

    @Override
    public Map<String,Object> alpayMchntInfoSelect2List(HttpServletRequest request) throws RedpException {
        try {
            String q = request.getParameter("q");
            if (null == q) {
                return null;
            }
            String instId = null;
            String mchntNo = null;
            q = q.replaceAll("&amp;", "&");
            String[] param = q.split("&");
            if (param.length > 0) {
                mchntNo = param[0];
            }
            if (param.length > 1) {
                instId = param[1];
            }

            if (StringUtils.isBlank(instId)) {
                UpmpUserInfoVO user= CommonUtils.getCurrentUser(upmpSysParamService);
                instId = user.getInstId();
            }
            List<Map> list1 =this.baseMapper.alpayMchntInfoSelect2List(instId,mchntNo);
            Map<String, Object> maps = new HashMap<>();
            Map<String, String> map;
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                map = new HashMap<>();
                map.put("name", list1.get(i).get("MCHNT_NAME").toString());
                map.put("id", list1.get(i).get("MCHNT_NO").toString());
                list.add(map);
            }
            maps.put("results", list);
            maps.put("more", false);
            return maps;
        } catch (Exception e) {
            // TODO: handle exception
            log.error("交易出现异常，异常信息：" + e.getMessage());
            throw new RedpException("交易出现异常，异常信息：" + e.getMessage());
        }
    }
}
